package com.cqu.hospitalsystem.controller;

import com.alibaba.fastjson.JSONObject;
import com.cqu.hospitalsystem.entity.Office;
import com.cqu.hospitalsystem.service.OfficeService;
import com.cqu.hospitalsystem.utils.result.DataResult;
import com.cqu.hospitalsystem.utils.result.code.Code;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("office")
@Tag(name = "OfficeController", description = "科室管理")
public class OfficeController {
    /**
     * 服务对象
     */
    @Resource
    private OfficeService officeService;

    @PostMapping("allOffice")
    @Operation(summary = "获取所有科室", description = "查询所有科室")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到科室")
    })
    public DataResult allOffice() {
        List<Office> res = this.officeService.allOffice();
        if (res != null) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("officeList", res);
            System.out.println(jsonObject);
            return DataResult.successByData(jsonObject);
        } else {
            return DataResult.errByErrCode(Code.ACCOUNT_ERROR);
        }
    }

    @GetMapping
    @Operation(summary = "分页查询科室", description = "根据筛选条件和分页信息查询科室")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "400", description = "查询失败")
    })
    public ResponseEntity<Page<Office>> queryByPage(Office office, PageRequest pageRequest) {
        return ResponseEntity.ok(this.officeService.queryByPage(office, pageRequest));
    }

    @GetMapping("{id}")
    @Operation(summary = "根据ID查询科室", description = "根据科室ID查询单个科室")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "科室不存在")
    })
    public ResponseEntity<Office> queryById(@PathVariable("id") @Parameter(description = "科室ID") Long id) {
        return ResponseEntity.ok(this.officeService.queryById(id));
    }

    @PostMapping
    @Operation(summary = "新增科室", description = "创建新的科室")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "201", description = "创建成功"),
            @ApiResponse(responseCode = "400", description = "创建失败")
    })
    public ResponseEntity<Office> add(@RequestBody @Parameter(description = "科室对象") Office office) {
        return ResponseEntity.ok(this.officeService.insert(office));
    }

    @PutMapping
    @Operation(summary = "编辑科室", description = "更新科室信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "400", description = "更新失败")
    })
    public ResponseEntity<Office> edit(@RequestBody @Parameter(description = "科室对象") Office office) {
        return ResponseEntity.ok(this.officeService.update(office));
    }

    @DeleteMapping
    @Operation(summary = "删除科室", description = "根据科室ID删除科室")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "400", description = "删除失败")
    })
    public ResponseEntity<Boolean> deleteById(@Parameter(description = "科室ID") Long id) {
        return ResponseEntity.ok(this.officeService.deleteById(id));
    }
}